package com.gopay.bis.web.controller.ebank;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.bank.KhPayConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.khpay.ebank.KhPayMessage;

/**
 * 平安跨行支付接口后台通知处理类
 * 
 * @KhPayServerRes.java
 * @author zhengxue
 * @2017-8-16 下午4:34:02 www.gopay.com.cn Inc.All rights reserved.
 */
@Controller
@Scope("prototype")
public class KhPayServerRes extends RefundBankResBase {

    private static Log logger = LogFactory.getLog(KhPayServerRes.class);

    @RequestMapping(value = "/khPayServerRes")
    protected ModelAndView execute(HttpServletRequest request, HttpServletResponse response) {
        logger.error("--------------- KhPayServerRes bankback  ---------------");
        return super.execute(request, response);
    }
    
    @RequestMapping(value = "/khPayRefundServerRes")
    protected void executeRefund(HttpServletRequest request,HttpServletResponse response) {
        logger.error("--------------- KhPayServerRes executeRefund  bankback---------------");
        super.executeRefund(request, response);
    }

    @Override
    protected BaseMessage buildMessage(HttpServletRequest request) {
        KhPayMessage msg = new KhPayMessage();
        String plain = request.getParameter("orig");
        String sign = request.getParameter("sign");

        msg.setBank(BankConstants.BANK_LOGO_KHPAY);
        msg.setPlainMessage(plain);
        msg.setSignMessage(sign);
        // 银行业务产品00个人网银支付
        msg.setBankBusProd(BisBankBusinessProd._00);
        // 银行接口通道信息 20: 平安跨行支付
        msg.setBankIntrChann(BisBankInterfaceChannel._20);
        return msg;
    }

    @Override
    protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
        OrderInfo orderInfo = new OrderInfo();
        KhPayMessage khPMessage = (KhPayMessage) result.getModel("res");
        if (khPMessage.isVerifyResult() && KhPayConstants.STATUS_SUCCESS.equals(khPMessage.getStatus())) {
            orderInfo.setOrderId(khPMessage.getOrderId());// 为了获取系统订单号
            orderInfo.setAmt(khPMessage.getAmount().toString());
            orderInfo.setNotifyNum("1");
            orderInfo.setPayStatus("00");
            orderInfo.setRetCode("00");
            orderInfo.setRetMsg("success");
        } else {
            orderInfo.setOrderId(khPMessage.getOrderId());// 为了获取系统订单号
            orderInfo.setAmt(khPMessage.getAmount().toString());
            orderInfo.setNotifyNum("1");
            orderInfo.setPayStatus("01");
            orderInfo.setRetCode("01");
            orderInfo.setRetMsg("");
        }
        return orderInfo;
    }

    @Override
    protected boolean checkParameters(HttpServletRequest request) {
        if (StringUtils.isEmpty(request.getParameter("orig")) || StringUtils.isEmpty(request.getParameter("sign"))) {
            return false;
        }
        return true;
    }

    @Override
    protected boolean checkRefundParameters(HttpServletRequest request) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    protected BaseMessage buildRefundMessage(HttpServletRequest request) {
        // TODO Auto-generated method stub
        return null;
    }
}
